package com.fnzn.entranceGuard.service.power.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fnzn.entranceGuard.entity.power.*;
import com.fnzn.entranceGuard.mapper.power.PowerBaseMapper;
import com.fnzn.entranceGuard.service.power.*;
import com.fnzn.entranceGuard.util.DateTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sxf
 * @since 2019-01-03
 */
@Service
@Slf4j
public class PowerBaseServiceImpl extends ServiceImpl<PowerBaseMapper, PowerBase> implements IPowerBaseService {
    @Autowired
    private PowerBaseMapper baseMapper;
    @Autowired
    private IPowerHourService hourService;
    @Autowired
    private IPowerDayService dayService;
    @Autowired
    private IPowerMonthService monthService;
    @Autowired
    private IPowerDeviceService powerDeviceService;

    /**
     * 插入进出表数据
     * 顺序更新 时 日 月数据
     *
     * @param base
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void uninUpdate(PowerBase base) {
        Integer year = DateTools.getCurrentYear();
        Integer month = DateTools.getCurrentMonth();
        Integer day = DateTools.getCurrentDay();
        Integer hour = DateTools.getCurrentHour();
        String date = DateTools.getCurrentSysData(DateTools.SIMPLE_FORMAT);
        /**
         * 更新总电量，同时计算电表增量
         */
        PowerDevice device = powerDeviceService.getPowerById(base.getDeviceId(), base.getPowerId());

        BigDecimal increment =  base.getTotalPower();
        /**
         * 查询是否有断电异常
         * 断电时长大于1小时，则把总电量平均分配到小时
         */
        if (device != null && device.getUpdateTime() != null) {
            int interval = DateTools.datePoorDiffMinute(device.getUpdateTime(), new Date());
            if (interval > 60) {
                int average = interval / 60 + 1;
                increment = (base.getTotalPower().subtract(device.getTotalPower())).divide(new BigDecimal(average),2,BigDecimal.ROUND_HALF_UP);
            } else {
                increment =  base.getTotalPower().subtract(device.getTotalPower() == null ? new BigDecimal(0) : device.getTotalPower());
            }
        }
        //log.info("电表增量："+increment);
        if (device != null) {
            device.setTotalPower(base.getTotalPower());
            device.setUpdateTime(new Date());
            powerDeviceService.updateById(device);
        }
        baseMapper.insert(base);
        /**
         * 更新小时表
         */
        PowerHour powerHour = hourService.getByDateHour(base.getDeviceId(), date, hour,base.getPowerId());

        if (powerHour == null) {
            powerHour = new PowerHour();
            powerHour.setDate(date);
            powerHour.setHour(hour);
            powerHour.setPowerId(base.getPowerId());
            powerHour.setMaxPower(base.getPower());
            powerHour.setMinPower(base.getPower());
            powerHour.setDeviceId(base.getDeviceId());
            powerHour.setPowerId(base.getPowerId());
            powerHour.setPower(increment);
            hourService.insert(powerHour);
        } else {
            if (base.getPower() > powerHour.getMaxPower()) {
                powerHour.setMaxPower(base.getPower());
            }
            if (base.getPower() < powerHour.getMinPower()) {
                powerHour.setMinPower(base.getPower());
            }
            powerHour.setPower((powerHour.getPower() == null ? new BigDecimal(0) : powerHour.getPower()).add(increment));
            powerHour.setUpdateTime(new Date());
            hourService.updateById(powerHour);
        }
        /**
         * 更新日表
         */
        PowerDay powerDay = dayService.getByDeviceDate(base.getDeviceId(), year, month, day,base.getPowerId());
        if (powerDay == null) {
            powerDay = new PowerDay();
            powerDay.setDeviceId(base.getDeviceId());
            powerDay.setDay(day);
            powerDay.setYear(year);
            powerDay.setMonth(month);
            powerDay.setMaxPower(base.getPower());
            powerDay.setMinPower(base.getPower());
            powerDay.setPower(increment);
            powerDay.setPowerId(base.getPowerId());
            dayService.insert(powerDay);
        } else {
            if (base.getPower() > powerDay.getMaxPower()) {
                powerDay.setMaxPower(base.getPower());
            }
            if (base.getPower() < powerDay.getMinPower()) {
                powerDay.setMinPower(base.getPower());
            }
            powerDay.setPower((powerDay.getPower() == null ? new BigDecimal(0) : powerDay.getPower()).add(increment));
            powerDay.setUpdateTime(new Date());
            dayService.updateById(powerDay);
        }

        /**
         * 更新月表
         */
        PowerMonth powerMonth = monthService.getByDeviceDate(base.getDeviceId(), year, month,base.getPowerId());
        if (powerMonth == null) {
            powerMonth = new PowerMonth();
            powerMonth.setDeviceId(base.getDeviceId());
            powerMonth.setYear(year);
            powerMonth.setMonth(month);
            powerMonth.setMaxPower(base.getPower());
            powerMonth.setMinPower(base.getPower());
            powerMonth.setPower(increment);
            powerMonth.setPowerId(base.getPowerId());
            monthService.insert(powerMonth);
        } else {
            if (base.getPower() > powerMonth.getMaxPower()) {
                powerMonth.setMaxPower(base.getPower());
            }
            if (base.getPower() < powerMonth.getMinPower()) {
                powerMonth.setMinPower(base.getPower());
            }
            powerMonth.setPower((powerMonth.getPower() == null ? new BigDecimal(0) : powerMonth.getPower()).add(increment));
            powerMonth.setUpdateTime(new Date());
            monthService.updateById(powerMonth);
        }
    }

    public static void main(String[] args) {
        int i = (int) (17.99 / 6);
        System.out.println(i);
    }
}
